﻿#region Imported Namespaces

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using BO = PHSRAG.Insight21.BusinessObjects;
    using Utility = PHSRAG.Utility;
    using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
    using DataAccess = PHSRAG.Utility.DataAccess;

#endregion Imported Namespaces

namespace PHSRAG.Insight21.COI
{
    public class COIAudit : BO.SmartBusinessObject
    {

        #region Constants

            private const string coiAuditKey = "COIAudit_2B547DE3-AF46-4b7b-9997-AC8197511560";
            private const string ColumnId = "Id";
            private const string ColumnCOIId = "COIId";
            private const string ColumnAction = "Action";
            private const string ColumnAdmPersonId = "AdmPersonId";
            private const string ColumnActionDate = "ActionDate";

        #endregion Constants

        #region Private Variables

            private int coiAuditId;
            private int coiId;
            private string action;
            private int admPersonId;
            private DateTime actionDate;

        #endregion Private Variables

        #region Properties

            public int COIAuditId
            {
                get { return coiAuditId; }
            }

            public int COIId
            {
                get { return coiId; }
                set { SetDirty(ref coiId, value); }
            }
            public string Action
            {
                get { return action; }
                set { SetDirty(ref action,value);}
            }

            public int AdmPersonId
            {
                get { return admPersonId; }
                set { SetDirty(ref admPersonId,value);}
            }

            public DateTime ActionDate
            {
                get { return actionDate; }
            }
        #endregion Properties

        #region Static Members

        /// <summary>
        /// Key used to place and/or retrieve this object from the cache.
        /// </summary>
        public static string COIAuditKey
        {
            get { return coiAuditKey; }
        }

        #endregion Static Members


        #region Constructors
		/// <summary>
		/// Constructs a new COIAudit object based on the Cache Policy passed in as a parameter. 
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		public COIAudit(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		}


		/// <summary>
		/// Constructs a new COIAudit object based on the Cache Policy passed in as a parameter. 
		/// All the instance variables will be set to the corresponding values from the data rows.
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		/// <param name="dr">DataRow representing a COIOrganization</param>
        public COIAudit(ICachePolicy cachePolicy, DataRow dr) : base(cachePolicy, dr)
		{
			PopulateMembers(dr);
		}
		#endregion

        #region SmartBusinessObject methods
        /// <summary>
        /// Cache Key uniquely identifies this object within the cache.
        /// </summary>
        /// <returns>the unique string for this object.</returns>
        public override string CacheKey()
        {
            return COIAuditKey;
        }


        /// <summary>
        /// Cache this instance using the prevalent cache policy.
        /// </summary>
        public override void Cache()
        {
            CachePolicy.Cache(COIAuditKey, this);
        }


        /// <summary>
        /// Remove this instance from the cache using the prevalent cache policy.
        /// </summary>
        public override void UnCache()
        {
            CachePolicy.UnCache(COIAuditKey);
        }


        /// <summary>
        /// Loads the COIOrganization object with the specified id.
        /// Note: This method is not yet implemented.
        /// </summary>
        /// <param name="connectionString">Information to connect to the database</param>
        /// <param name="args"></param>
        public override void Load(string connectionString, params object[] args)
        {
            throw new Exception("Not implemented");
        }


        /// <summary>
        /// Saves the state of the COIOrganization object to the database. 
        /// The stored procedure inserts a new record if the id of the object is 0, 
        /// otherwise the procedure updates the record with the matching COIOrganization id
        /// </summary>
        /// <param name="sqlTransaction">The context in which the insert/update should be executed</param>
        protected override void SaveObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
        {
            try
            {
                SqlParameter[] paramArray = DataAccess.BuildSqlParameterArray(
                    "@Id", SqlDbType.Int, Id,
                    "@COIId", SqlDbType.VarChar, coiId,
                    "@Action", SqlDbType.VarChar, ((action == string.Empty) ? (object)DBNull.Value : action),
                    "@AdmPersonId", SqlDbType.Int, admPersonId);
                    

                Id = Convert.ToInt32(DataAccess.ExecuteScalar(sqlTransaction.Connection, sqlTransaction, "SaveCOIAudit",
                    paramArray));

            }
            catch (Exception e)
            {
                throw new Exception("Failed to save the COIAudit", e);
            }
        }


        /// <summary>
        /// Deletes the ProtocolDocument from the database.
        /// Note: This method is not yet implemented.
        /// </summary>
        /// <param name="sqlTransaction">The transaction context in which the SQL statement is to be executed</param>
        protected override void DeleteObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
        {
            throw new Exception("Not implemented");
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Populates instance fields with corresponding values in the datarow
        /// </summary>
        /// <param name="dataRow">DataRow holding the values to be assigned to instance variables</param>
        private void PopulateMembers(DataRow dataRow)
        {
            Id = (int)dataRow[ColumnId];
            coiId = (int)dataRow[ColumnCOIId];
            Object dbValue = dataRow[ColumnAction];
            action = (dbValue == DBNull.Value) ? string.Empty : (string)dbValue;
            admPersonId = (int)dataRow[ColumnAdmPersonId];
            actionDate = (DateTime)dataRow[ColumnActionDate];
        }

        #endregion Private Methods
    }
}
